﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    class JLTAB05DOCReport : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        string ABPAUFFKRS;
        string ABPAUFFLFD;
        string ABPAUFFPOS;
        string DocType;
        string CellName = "";
        string CellValue = "";
        public string LAYOUTTYPE = "";
        public string folge = "";
        string strCompany;
        string strExcelFile;
        Dictionary<string, int> qpplan = new Dictionary<string, int>();
        
        #endregion
        public JLTAB05DOCReport()
            : base()
        {
        }
        public JLTAB05DOCReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            if (dicParamater.ContainsKey("ROOTDRIVE"))
            {
                Root = dicParamater["ROOTDRIVE"].ToString();
            }
            else
            {
                Root = "C:\\IntexReport";
            }
                      						

        } 
        #region------------------------------Function------------------------
        /*ABEAUFKRS	ABEAUFLFD	ABEAUFPOS	ABPART	ABPFARBE	ABPBESCHICH	BEZ	ABFAFOLGE	ABFAGANG	
         * STEP	ABEMODNR	AMEBEZ	ABEWERTX	AMEMASS	DIFF	WIDTH	YARN	SS*/
        public void AB05TECH(DataSet setSoruce)
        {
            try
            {
                int x = 0, y = 0;
                DataTable dtSoruce = setSoruce.Tables["ABPOS"];
                DataRow temp = dtSoruce.Rows[0];
                string orderno = temp["ABEAUFKRS"].ToString().Trim() + "-"
                    + System.Convert.ToInt64(temp["ABEAUFLFD"].ToString().Trim());
                string art = temp["ABPART"].ToString().Trim();
                Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
                string process = temp["BEZ"].ToString().Trim();
                string diff = temp["DIFF"].ToString().Trim();
                string color = temp["COLOR"].ToString().Trim();
                exAgent.SetCellValue(4, 5, orderno);
                exAgent.SetCellValue(4, 13, art);
                exAgent.SetCellValue(4, 20, color);
                exAgent.SetCellValue(4, 33, date);
                exAgent.SetCellValue(6, 5, process);
                exAgent.SetCellValue(32, 1, diff);
                string value = "",step = "",amebez = "",ameass = "";
                string folge = "";
                string newfolge = "";
                x = 10; 
                int i = 0,j = 0;
                foreach( DataRow dw in dtSoruce.Rows)
                {
                    y = 5;
                    newfolge = dw["ABFAFOLGE"].ToString().Trim();
                    step = dw["STEP"].ToString().Trim();
                    amebez = dw["AMEBEZ"].ToString().Trim();
                    value = dw["ABEWERTX"].ToString().Trim();
                    ameass = dw["AMEMASS"].ToString().Trim();
                    if (newfolge == folge)
                    {
                        i = 0;                                                                       
                        if (j == 0)
                        {
                            exAgent.SetCellValue(x , y , amebez);
                            exAgent.SetCellValue(x , y + 8 , value + " " + ameass);
                            j = 1;
                        }
                        else
                        {
                            exAgent.SetCellValue(x , y + 16, amebez);
                            exAgent.SetCellValue(x , y + 24, value + " " + ameass);
                            j = 0;
                            i = 1;
                        }
                        if (i == 1)
                        {
                            x = x + 1;
                        }
                        i = 1;
                    }
                    else
                    {
                        if (i == 1&&j==1)
                        {
                            x = x + 1;
                        }                        
                        exAgent.SetCellValue( x ,1 , step);
                        i = 1;
                        exAgent.SetCellValue(x, y, amebez);
                        exAgent.SetCellValue(x, y + 8, value + " " + ameass);
                        if(j == 0)
                        {                           
                            j = 1;
                        } 
                    }
                    folge = newfolge;
                }
                						
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void AB05PS(DataSet setSoruce)
        {
            try
            {
                int x = 0, y = 0;
                DataTable dtSoruce = setSoruce.Tables["ABPOS"];
                DataRow temp = dtSoruce.Rows[0];
                string orderno = temp["ABEAUFKRS"].ToString().Trim() + "-"
                    + System.Convert.ToInt64(temp["ABEAUFLFD"].ToString().Trim());
                string art = temp["ABPART"].ToString().Trim();
                Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
                string process = temp["BEZ"].ToString().Trim();
                string diff = temp["DIFF"].ToString().Trim();
                string color = temp["COLOR"].ToString().Trim();
                string width = temp["WIDTH"].ToString().Trim();
                string yarn = temp["YARN"].ToString().Trim();
                string ss = temp["SS"].ToString().Trim();
                exAgent.SetCellValue(4, 5, orderno);
                exAgent.SetCellValue(4, 13, art);
                exAgent.SetCellValue(4, 20, color);
                exAgent.SetCellValue(4, 36, date);
                exAgent.SetCellValue(5, 5, width);
                exAgent.SetCellValue(5, 16, yarn);
                exAgent.SetCellValue(5, 32, ss);
                exAgent.SetCellValue(6, 5, process);
                exAgent.SetCellValue(23, 1, diff);
                string value = "", step = "", amebez = "", ameass = "";
                string folge = "";
                string newfolge = "";
                x = 7;
                int i = 0, j = 0, k = 0, t = 0;
                foreach (DataRow dw in dtSoruce.Rows)
                {
                    y = 5;
                    newfolge = dw["ABFAFOLGE"].ToString().Trim();
                    step = dw["STEP"].ToString().Trim();
                    amebez = dw["AMEBEZ"].ToString().Trim();
                    value = dw["ABEWERTX"].ToString().Trim();
                    ameass = dw["AMEMASS"].ToString().Trim();
                    if (newfolge == folge)
                    {
                        i = 0;                        
                        if (j == 0)
                        {
                            exAgent.SetCellValue(x, y, amebez);
                            exAgent.SetCellValue(x, y + 5, value + " " + ameass);
                            j = 1;
                        }
                        else if (j == 1)
                        {
                            exAgent.SetCellValue(x, y + 12, amebez);
                            exAgent.SetCellValue(x, y + 17, value + " " + ameass);
                            j = 2; 
                            
                        }
                        else
                        {
                            exAgent.SetCellValue(x, y + 24, amebez);
                            exAgent.SetCellValue(x, y + 29, value + " " + ameass);
                            j = 0;
                            i = 1;
                        }
                        if (i == 1)
                        {
                            x = x + 1;
                        }
                        #region---工序记录---
                        if (k==1)
                        {
                            exAgent.SetCellValue(29 + t, 29, amebez + ":");
                            exAgent.SetCellValue(30+t, 29, "问题点记录:");
                            //exAgent.SetCellValue(29 + t, 34, value + " " + ameass);
                        }
                        if (k == 2)
                        {
                            exAgent.SetCellValue(39 + t, 29, amebez + ":");
                            exAgent.SetCellValue(40+t, 29, "问题点记录:");
                            //exAgent.SetCellValue(39 + t, 34, value + " " + ameass);
                        }
                        if (k == 3)
                        {
                            exAgent.SetCellValue(49 + t, 29, amebez + ":");
                            exAgent.SetCellValue(50+t, 29, "问题点记录:");
                            //exAgent.SetCellValue(49 + t, 34, value + " " + ameass);
                        }
                        if (k == 4)
                        {
                            exAgent.SetCellValue(66 + t, 29, amebez + ":");
                            exAgent.SetCellValue(67+t, 29, "问题点记录:");
                            //exAgent.SetCellValue(66 + t, 34, value + " " + ameass);
                        }
                        if (k == 5)
                        {
                            exAgent.SetCellValue(80 + t, 29, amebez + ":");
                            exAgent.SetCellValue(81+t, 29, "问题点记录:");
                            //exAgent.SetCellValue(80 + t, 34, value + " " + ameass);
                        }
                        if (k == 6)
                        {
                            exAgent.SetCellValue(94 + t, 29, amebez + ":");
                            exAgent.SetCellValue(95+t, 29, "问题点记录:");
                            //exAgent.SetCellValue(94 + t, 34, value + " " + ameass);
                        }
                        if (k == 7)
                        {
                            exAgent.SetCellValue(107 + t, 29, amebez + ":");
                            exAgent.SetCellValue(108+t, 29, "问题点记录:");
                            //exAgent.SetCellValue(107 + t, 34, value + " " + ameass);
                        }
                        t = t + 1;
                        #endregion
                    }
                    else
                    {   
                        if(j!=0)
                        {
                            x = x + 1;
                        }
                        k = k + 1;
                        t = 1;                        
                        exAgent.SetCellValue(x, 1, step);
                        exAgent.SetCellValue(x, y, amebez+":");
                        exAgent.SetCellValue(x, y + 5, value + " " + ameass);
                        j = 1;
                        #region--工序记录---
                        if (k==1)
                        {
                            exAgent.SetCellValue(28, 1, step);
                            exAgent.SetCellValue(29, 29, amebez + ":");
                            exAgent.SetCellValue(30, 29, "问题点记录:");
                            //exAgent.SetCellValue(29, 34, value + " " + ameass);
                        } 
                        if (k == 2)
                        {
                            exAgent.SetCellValue(39, 1, step);
                            exAgent.SetCellValue(39, 29, amebez + ":");
                            exAgent.SetCellValue(40, 29, "问题点记录:");
                            //exAgent.SetCellValue(39, 34, value + " " + ameass);
                        } 
                        if (k == 3)
                        {
                            exAgent.SetCellValue(49, 1, step);
                            exAgent.SetCellValue(49, 29, amebez + ":");
                            exAgent.SetCellValue(50, 29, "问题点记录:");
                            //exAgent.SetCellValue(49, 34, value + " " + ameass);
                        }
                        if (k == 4)
                        {
                            exAgent.SetCellValue(65, 1, step);
                            exAgent.SetCellValue(66, 29, amebez + ":");
                            exAgent.SetCellValue(67, 29, "问题点记录:");
                            //exAgent.SetCellValue(66, 34, value + " " + ameass);
                        }
                        if (k == 5)
                        {
                            exAgent.SetCellValue(80, 1, step);
                            exAgent.SetCellValue(80, 29, amebez + ":");
                            exAgent.SetCellValue(81, 29, "问题点记录:");
                            //exAgent.SetCellValue(80, 34, value + " " + ameass);
                        }
                        if (k == 6)
                        {
                            exAgent.SetCellValue(94, 1, step);
                            exAgent.SetCellValue(94, 29, amebez + ":");
                            exAgent.SetCellValue(95, 29, "问题点记录:");
                            //exAgent.SetCellValue(94, 34, value + " " + ameass);
                        }
                        if (k == 7)
                        {
                            exAgent.SetCellValue(107, 1, step);
                            exAgent.SetCellValue(107, 29, amebez + ":");
                            exAgent.SetCellValue(108, 29, "问题点记录:");
                            //exAgent.SetCellValue(107, 34, value + " " + ameass);
                        }
                        #endregion
                    }
                    folge = newfolge;
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void AB05MACHINE(DataSet setSoruce)
        {
            try
            {
                int x = 0, y = 0;
                //DataTable tbFOLGE = new DataTable();
                DataTable dtSoruce = setSoruce.Tables["ABPOS"];
                DataRow temp = dtSoruce.Rows[0];
                string orderno = temp["ABEAUFKRS"].ToString().Trim() + "-"
                    + System.Convert.ToInt64(temp["ABEAUFLFD"].ToString().Trim());
                string art = temp["ABPART"].ToString().Trim();
                Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
                string process = temp["BEZ"].ToString().Trim();
                string color = temp["COLOR"].ToString().Trim();
                string diff = temp["DIFF"].ToString().Trim();
                string step = temp["STEP"].ToString().Trim() + "机工艺、色光记录";
                exAgent.SetCellValue(3, 1, step);                    
                exAgent.SetCellValue(5, 5, orderno);
                exAgent.SetCellValue(5, 13, art);
                exAgent.SetCellValue(5, 20, color);
                exAgent.SetCellValue(5, 36, date);
                exAgent.SetCellValue(6, 5, process);
                exAgent.SetCellValue(10, 1, diff);                
                string value = "",amebez = "", ameass = "";
                x = 7;
                int i = 0, j = 0, t = 0;
                foreach (DataRow dw in dtSoruce.Rows)
                {
                    y = 5;
                    amebez = dw["AMEBEZ"].ToString().Trim();
                    value = dw["ABEWERTX"].ToString().Trim();
                    ameass = dw["AMEMASS"].ToString().Trim();
                    i = 0;
                    if (j == 0)
                    {
                        exAgent.SetCellValue(x, y, amebez);
                        exAgent.SetCellValue(x, y + 5, value + " " + ameass);
                        j = 1;
                    }
                    else if (j == 1)
                    {
                        exAgent.SetCellValue(x, y + 12, amebez);
                        exAgent.SetCellValue(x, y + 17, value + " " + ameass);
                        j = 2;

                    }
                    else
                    {
                        exAgent.SetCellValue(x, y + 24, amebez);
                        exAgent.SetCellValue(x, y + 29, value + " " + ameass);
                        j = 0;
                        i = 1;
                    }
                    if (i == 1)
                    {
                        x = x + 1;
                    }
                    exAgent.SetCellValue(13 + t, 29, amebez + ":");
                    exAgent.SetCellValue(30 + t, 29, amebez + ":");
                    exAgent.SetCellValue(53 + t, 29, amebez + ":");
                    exAgent.SetCellValue(67 + t, 29, amebez + ":");
                    exAgent.SetCellValue(81 + t, 29, amebez + ":");
                    t = t + 1;
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void AB05M(DataSet setSoruce)
        {
            try
            {
                int x = 0, y = 0;
                //DataTable tbFOLGE = new DataTable();
                DataTable dtSoruce = setSoruce.Tables["ABPOS"];
                DataRow temp = dtSoruce.Rows[0];
                string orderno = temp["ABEAUFKRS"].ToString().Trim() + "-"
                    + System.Convert.ToInt64(temp["ABEAUFLFD"].ToString().Trim());
                string art = temp["ABPART"].ToString().Trim();
                Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
                string process = temp["BEZ"].ToString().Trim();
                string color = temp["COLOR"].ToString().Trim();
                string diff = temp["DIFF"].ToString().Trim();
                string step = temp["STEP"].ToString().Trim() + "机工艺、色光记录";
                exAgent.SetCellValue(5, 5, orderno);
                exAgent.SetCellValue(5, 13, art);
                exAgent.SetCellValue(5, 20, color);
                exAgent.SetCellValue(5, 36, date);
                exAgent.SetCellValue(6, 5, process);
                exAgent.SetCellValue(10, 1, diff);
                string value = "", amebez = "", ameass = "";
                x = 7;
                int i = 0, j = 0;
                foreach (DataRow dw in dtSoruce.Rows)
                {
                    y = 5;
                    amebez = dw["AMEBEZ"].ToString().Trim();
                    value = dw["ABEWERTX"].ToString().Trim();
                    ameass = dw["AMEMASS"].ToString().Trim();
                    i = 0;
                    if (j == 0)
                    {
                        exAgent.SetCellValue(x, y, amebez);
                        exAgent.SetCellValue(x, y + 5, value + " " + ameass);
                        j = 1;
                    }
                    else if (j == 1)
                    {
                        exAgent.SetCellValue(x, y + 12, amebez);
                        exAgent.SetCellValue(x, y + 17, value + " " + ameass);
                        j = 2;

                    }
                    else
                    {
                        exAgent.SetCellValue(x, y + 24, amebez);
                        exAgent.SetCellValue(x, y + 29, value + " " + ameass);
                        j = 0;
                        i = 1;
                    }
                    if (i == 1)
                    {
                        x = x + 1;
                    }
                    
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }        
        public override void WriteExcelReport(string fileType)
        {
            bool IsFTP = false;
            try
            {
                strCompany = "JLT";
                strExcelFile = @"JLT\JLTProduction.xls";
                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\JLT\JLTProduction.xls");
                destfolder = SetFolder(Root, "后整理");
                DataRow dwREPORT = dsSource.Tables["ABPOS"].Rows[0];
                string ABEAUFKRS = dwREPORT["ABEAUFKRS"].ToString().Trim();
                string ABEAUFLFD = dwREPORT["ABEAUFLFD"].ToString().Trim();
                //string ABEAUFPOS = dwREPORT["ABEAUFPOS"].ToString().Trim();
                string orderno = ABEAUFKRS + ABEAUFLFD;
                //DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                string newfilename = "";
                if(LAYOUTTYPE == "1")
                {
                    newfilename = @"整理工艺" + orderno;
                }
                if (LAYOUTTYPE == "2")
                {
                    newfilename = @"生产样工艺卡" + orderno;
                }
                if (LAYOUTTYPE == "3")
                {
                    newfilename = @"机台工艺卡" + orderno+"-"+folge;
                } 
                if (LAYOUTTYPE == "4")
                {
                    newfilename = @"磨毛工艺卡" + orderno + "-" + folge;
                } 
                if (LAYOUTTYPE == "5")
                {
                    newfilename = @"抓毛工艺卡" + orderno + "-" + folge;
                }
                exAgent.CheckWorkBook(newfilename);
                ServerShareFile("", year, month, newfilename, fileType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ServerShareFile(string KRS, string year, string month, string ordernumer, string fileType)
        {
            string destfile, tempfile = "";
            string fileabsname = ordernumer;
            try
            {
                filename = destfolder + "\\" + fileabsname + ".xls"; ;
                destfile = filename;
                tempfile = CreateFile(ordernumer);
                if (File.Exists(destfile))
                {
                    File.Delete(destfile);
                }
                File.Move(tempfile, destfile);

                if (fileType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (File.Exists(tempfile))
                {
                    File.Delete(tempfile);
                }
            }
        }
        private string CreateFile(string orderno)
        {
            string filename = "";
            try
            {

                if(LAYOUTTYPE =="1")
                {
                    Standard = "AB05TECH";
                }
                if (LAYOUTTYPE == "2")
                {
                    Standard = "AB05PS";
                }
                if (LAYOUTTYPE == "3")
                {
                    Standard = "AB05MACHINE";
                }
                if (LAYOUTTYPE == "4")
                {
                    Standard = "AB05MM";
                }
                if (LAYOUTTYPE == "5")
                {
                    Standard = "AB05ZM";
                }
                string pwd = "";
                string sheetName = orderno;
                exAgent.Open(TemplatePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                exAgent.CreateNewSheet(orderno + ".xls", Standard, sheetName, pwd);
                filename = exAgent.FileName;
                exAgent.SetCurrentSheet(1);
                if(LAYOUTTYPE =="1")
                {
                    AB05TECH(dsSource);
                }
                if (LAYOUTTYPE == "2")
                {
                    AB05PS(dsSource);
                }
                if (LAYOUTTYPE == "3")
                {
                    AB05MACHINE(dsSource);
                }
                if (LAYOUTTYPE == "4")
                {
                    AB05M(dsSource);
                }
                if (LAYOUTTYPE == "5")
                {
                    AB05M(dsSource);
                } 
                exAgent.Save();
                exAgent.Close();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return filename;
        }
        #endregion
    }
}